#pragma once


#pragma warning(disable:4786)
#include <map>

#include "../Inc/IocpServer.h"
#include "..\..\Lib_Base\Inc\SyncObjs.h"
#include "../Inc/ISocket.h"

namespace net
{
	int m_ServePort;
	class CSocket;
	class CServeSocket : public IServeSocket,public IIocpServerEvent
	{
	// Constructor
	public:
		CServeSocket(IServeSocketEvent& iSocketEvent);
		virtual ~CServeSocket();
		
		bool					Init			(int nServePort,const char* IP);


		// interface of IIocpServerEvent
	public:
		virtual int				OnRcvMsg		(int AutoId, const char* buf, int nLen);
		virtual int				OnAcceptSck		(SOCKET socket);
		virtual void			OnCloseSck		(int AutoId);
		virtual void			OnSendMsgContinue(int Autod);

	// interface of IServeSocket
	public:
		virtual unsigned long	Release			(void)			{ delete this; return 0; }
		
		virtual bool			SendMsg			(sbase::IMessage * pSendBuf);

		virtual bool			DisConnectClient(int socket);
		virtual void			RefuseConnect	(bool bEnable)	{ m_bRefuseConnect = bEnable; }
		

		virtual int	GetSocketAmount	(char type)			
		{ 
			if (type==0)
			  return int(m_setSocket.size()); 
			else
			  return m_iocpServer->GetClientSockCount(); 
		}

	protected:  
		CSocket*				GetSocket		(int socket);
		void					CloseAll		(void);
		
	protected:
		IServeSocketEvent&		m_eventSocket;
		CIocpServer *           m_iocpServer;
		bool					m_bRefuseConnect;
		
		typedef	std::map<int, CSocket*>	SOCKET_MAP;
		SOCKET_MAP				m_setSocket;

	private:
		sbase::CCriticalSection	m_cs;
		int m_LastAutoId;
	};	
}

